package com.soecode.lyf.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soecode.lyf.entity.User;
import com.soecode.lyf.tool.AjaxResult;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class BaseController {
    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     * @param message 成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }

    /**
     * 返回成功数据
     * @param data 成功对象
     */
    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

    /**
     * 返回失败消息
     * @param message 失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 页面跳转
     */
    public String redirect(String url)
    {
        return "redirect:"+url;
    }

    /**
     * 获得资源目录下的真实路径
     * @param path 资源Resource 目录下的相对路径
     * @return 资源目录的真实路径
     */
    public String getResourcePath(String path) throws IOException {
        return new ClassPathResource(path).getFile().getPath();
    }

    public IPage page(long current, long size, BaseMapper mapper, QueryWrapper queryWrapper) {
        IPage iPage = new Page(current,size);
        IPage page =  mapper.selectPage(iPage,queryWrapper);
        return page;
    }
}
